package com.alibaba.citrus.cr.order.open.api.facade.transferforecastorder;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.citrus.cr.order.open.api.facade.model.TransferAndForecastOrder;
import com.alibaba.citrus.cr.order.open.api.facade.model.TransferAndForecastOrderQueryRequest;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crunifiedinventory.domain.allocateapplyorder.dto.LoadAllocateApplyOrderRequest;
import com.epoch.app.crunifiedinventory.domain.allocateapplyorder.model.AllocateApplyOrder;
import com.epoch.app.crunifiedinventory.domain.allocateapplyorder.service.AllocateApplyOrderService;
import com.epoch.app.crunifiedinventory.domain.allocateapplyorderdetail.dto.AllocateApplyOrderDetailLoadListRequest;
import com.epoch.app.crunifiedinventory.domain.allocateapplyorderdetail.model.AllocateApplyOrderDetail;
import com.epoch.app.crunifiedinventory.domain.allocateapplyorderdetail.service.AllocateApplyOrderDetailService;
import com.epoch.app.crunifiedinventory.domain.allocateorder.dto.AllocateOrderLoadListRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorder.model.AllocateOrder;
import com.epoch.app.crunifiedinventory.domain.allocateorder.service.AllocateOrderService;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.QueryListByIdsRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.QueryListByIdsResponse;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.model.AllocateOrderDetail;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.service.AllocateOrderDetailService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: yuhw
 * @date: 2024年02月29日 19:57
 **/
@Component
@Primary
public class TransferForecastOrderQueryServiceImpl implements TransferForecastOrderQueryService{
    private static Log log = Log.getLogger(TransferForecastOrderQueryServiceImpl.class);

    @Resource
    private AllocateApplyOrderService allocateApplyOrderService;
    @Resource
    private AllocateApplyOrderDetailService allocateApplyOrderDetailService;
    @Resource
    private AllocateOrderDetailService allocateOrderDetailService;
    @Resource
    private AllocateOrderService allocateOrderService;

    private static final Integer MAX_PAGE_SIZE = 1000;

    @Override
    @FacadeInvoker
    public Result<TransferAndForecastOrder> queryTransferAndForecastOrder(TransferAndForecastOrderQueryRequest request) {
        log.info("queryTransferAndForecastOrder_request:{}", JSON.toJSONString(request));
        Result<TransferAndForecastOrder> result = Result.success(null);
        if (request.getTransferForecastOrderId() == null){
            return result;
        }
        LoadAllocateApplyOrderRequest loadAllocateApplyOrderRequest = new LoadAllocateApplyOrderRequest();
        loadAllocateApplyOrderRequest.setId(request.getTransferForecastOrderId().toString());
        AllocateApplyOrder applyOrder = allocateApplyOrderService.loadAllocateApplyOrder(loadAllocateApplyOrderRequest);
        if (applyOrder == null){
            log.error("queryTransferAndForecastOrder_mot_match_applyOrder");
            return result;
        }
        TransferAndForecastOrder transferAndForecastOrder = new TransferAndForecastOrder();
        transferAndForecastOrder.setAllocateApplyOrder(applyOrder);
        result.setResult(transferAndForecastOrder);

        AllocateApplyOrderDetailLoadListRequest allocateApplyOrderDetailLoadListRequest = new AllocateApplyOrderDetailLoadListRequest();
        allocateApplyOrderDetailLoadListRequest.setAllocateApplyOrderId(request.getTransferForecastOrderId().intValue());
        allocateApplyOrderDetailLoadListRequest.setLimit(MAX_PAGE_SIZE);
        Result<List<AllocateApplyOrderDetail>> applyOrderDetailList = allocateApplyOrderDetailService.loadAllocateApplyOrderDetailList(allocateApplyOrderDetailLoadListRequest);
        if (applyOrderDetailList == null || CollectionUtils.isEmpty(applyOrderDetailList.getResult())){
            log.error("queryTransferAndForecastOrder_not_match_apply_detail");
            return result;
        }

        transferAndForecastOrder.setAllocateApplyOrderDetails(applyOrderDetailList.getResult());

        AllocateOrderLoadListRequest allocateOrderLoadListRequest = new AllocateOrderLoadListRequest();
        allocateOrderLoadListRequest.setAllocateApplyOrderId(request.getTransferForecastOrderId().toString());
        allocateOrderLoadListRequest.setLimit(MAX_PAGE_SIZE);
        Result<List<AllocateOrder>> listResult = allocateOrderService.loadAllocateOrderList(allocateOrderLoadListRequest);
        if (listResult == null || CollectionUtils.isEmpty(listResult.getResult())){
            log.error("queryTransferAndForecastOrder_not_match_order");
            return result;
        }
        transferAndForecastOrder.setAllocateOrders(listResult.getResult());

        QueryListByIdsRequest queryListByIdsRequest = new QueryListByIdsRequest();
        queryListByIdsRequest.setAllocateOrderIds(listResult.getResult().stream().map(AllocateOrder::getId).collect(Collectors.toList()));
        queryListByIdsRequest.setLimit(MAX_PAGE_SIZE);
        QueryListByIdsResponse queryListByIdsResponse = allocateOrderDetailService.queryListByIds(queryListByIdsRequest);
        if (queryListByIdsResponse == null || CollectionUtils.isEmpty(queryListByIdsResponse.getResult())){
            log.error("queryTransferAndForecastOrder_not_match_allocate_order_detail");
            return result;
        }
        List<AllocateOrderDetail> allocateOrderDetailList = queryListByIdsResponse.getResult();
        transferAndForecastOrder.setAllocateOrderDetails(allocateOrderDetailList);
        return result;
//        //构建一个更新入参
//        applyOrderDetailList.getResult().forEach(allocateApplyOrderDetail -> {
//            BigDecimal deliveryQuantity = allocateOrderDetailList.stream().filter(allocateOrderDetail ->
//                            Objects.equals(InfoUtil.deleteZeroPrefix(allocateApplyOrderDetail.getOutLineNo()), InfoUtil.deleteZeroPrefix(allocateOrderDetail.getOutAllocateApplyOrderLineId())))
//                    .map(AllocateOrderDetail::getDeliveryQuantity).filter(StringUtils::isNotBlank)
//                    .map(String::trim).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add);
//            String bizFeatures = allocateApplyOrderDetail.getBizFeatures();
//            Map<String, String> bizFeaturesMap = FeatureUtil.fromString(bizFeatures);
//            bizFeaturesMap.put(FeaturesKey.CUMULATIVE_CONSIGNMENT_QUANTITY,deliveryQuantity.toString());
//            UpdateFeaturesRequest updateFeaturesRequest = UpdateFeaturesRequest.builder().
//                    mainOrderId(allocateApplyOrderDetail.getAllocateApplyOrderId().toString())
//                    .lineNo(allocateApplyOrderDetail.getLineNo())
//                    .bizFeatures(FeatureUtil.toString(bizFeaturesMap))
//                    .build();
//            allocateApplyOrderDetailService.updateFeatures(updateFeaturesRequest);
//        });
    }
}
